# """
# @Project:education
# @File:rbac.py
# @Author:袁浩天
# @Date:15:46
# """
# from django.shortcuts import HttpResponse
# from django.utils.deprecation import MiddlewareMixin
# import jwt,time
# from jwt.exceptions import ExpiredSignatureError,DecodeError
# from django.http import JsonResponse
# from django.conf import settings
# import re
#
# # 路由中间件 RBAC 鉴权中间件
# class MyRBAC(MiddlewareMixin):
#     # 处理请求
#     def process_request(self,request):
#         try:
#             path = request.path_info
#             method = request.method.lower()  # GET = get
#             print(">>>>>>>>>>>>>>>>>>>>>>>>>>",method,path)
#
#             if re.findall("login|image|mobile_code", path, re.I):
#                 return None
#             token = request.headers.get("Authorization")
#             print("token", token)
#             try:
#                 payload = jwt.decode(token, key=settings.SECRET_KEY, algorithms=['HS256'])
#                 request.session["user"] = payload
#                 print("user>>>>", payload)
#             except ExpiredSignatureError:
#                 return JsonResponse({"code": 401, "message": "token过期"})
#             except DecodeError:
#                 return JsonResponse({"code": 402, "message": "请重新登录"})
#
#             user_id = payload.get("id")
#             print(f"访问用户{user_id}")
#             user = request.session.get("user")
#             print("获取到的session", user)
#             print("获取到的session", user.get("id"))
#         except Exception as e:
#             print("请求中间件错误",e)
#             return JsonResponse({"code": 500, "message": "服务器错误！"}, status=200)
#
#
#
#
#
#
#
#
#
#
